# -*- coding: utf-8 -*-
from flask import request, Blueprint, abort,render_template, session, redirect, url_for,flash
from forms import LoginForm
from flask_login import login_user,login_required,logout_user
from apps.models import ADMIN, nationnality,teacher,user_info,userlog
from apps import db

from config import ProjectConfig


from datetime import datetime,timedelta
import sys
from sqlalchemy import func
import redis

reload(sys)

sys.setdefaultencoding('utf-8')

account_bp = Blueprint('account_bp', __name__, template_folder='templates')

@account_bp.route('/',methods=['POST', 'GET'])
#登录
@account_bp.route('/big_data_login',methods=['POST', 'GET'])
def big_data_login():

    form = LoginForm()
    if request.method == "POST":
        if form.validate_on_submit():
            usernum = form.usernum.data
            password = form.password.data
            userinfo = ADMIN.query.filter_by(UID = usernum).first()
            if not userinfo:
                flash("用户名或密码错误，请重新输入！")
                return redirect(url_for('account_bp.big_data_login'))
            elif userinfo.PASSWD == password:
                session_name = ProjectConfig.APP_ID
                session[session_name] = userinfo.name
                login_user(userinfo)
                # flash("登录成功！")
                return redirect(url_for('account_bp.index'))
            elif not userinfo.PASSWD == password:
                flash("用户名或密码错误，请重新输入！")
                return redirect(url_for('account_bp.big_data_login'))
            else:
                flash("用户名或密码错误，请重新输入！")
                return redirect(url_for('account_bp.big_data_login'))
        flash("用户名或密码错误，请重新输入！")

        return redirect(url_for('account_bp.big_data_login'))
    elif request.method == "GET":
        return render_template('login.html', form=form)
        #return redirect (url_for ('wx_oauth.web_login',agent_id='1000015'))



#注销
@account_bp.route('/logout')
@login_required
def logout():
    from apps.oauth.views import web_logout
    #用户登出
    logout_user()
    flash(u'用户已退出登录')
    web_logout ()
    return redirect(url_for('account_bp.big_data_login'))






#首页
@account_bp.route('/index',methods=['POST', 'GET'])
#@login_required
#@authority('admin,index,other')
def index():
    session_name = ProjectConfig.APP_ID
    name = session.get (session_name)







    return render_template('index.html',name=name)

@account_bp.route('/about',methods=['POST', 'GET'])
#@login_required
#@authority('admin,about,other')
def about():
    session_name = ProjectConfig.APP_ID
    name = session.get(session_name)
    return render_template('about.html',name=name)

@account_bp.route('/warning',methods=['POST','GET'])

def warning():
    session_name = ProjectConfig.APP_ID
    name = session.get(session_name)
    return render_template('warning.html',name=name)

@account_bp.route('/blank',methods=['POST','GET'])

def blank():
    return render_template('blank.html')






@account_bp.route('/test',methods=['POST','GET'])
def test():
    return render_template('test.html')






























